package ch.manage.movie.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSplitPane;

import ch.manage.movie.business.Manager;
import ch.manage.movie.business.Movie;
import ch.manage.movie.informationFetcher.AFetchMoviesFromIMDB;
import ch.manage.movie.informationFetcher.Fetcher;

public class ManageMovieGui extends JFrame {
	private static final long serialVersionUID = 1L;
	private List<Movie> movieList;

	public ManageMovieGui() {
		super("Manage Movies");

		Manager manager = new Manager();
		movieList = manager.loadMovies();

		// Design and dimension
		setMinimumSize(new Dimension(1000, 600));

		MovieInfo movieInfo = new MovieInfo();
		final MovieList movieListGui = new MovieList(movieInfo, movieList);

		// Menu Bar
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("datei");
		menuBar.add(menu);

		ImageIcon imdb = new ImageIcon("./icons/imdb.png");
		JMenuItem imdbInforFetch = new JMenuItem("Fetch Info from IMDB", imdb);
		imdbInforFetch.addActionListener(new ActionListener() {
			Thread tMovieFetcher;
			Thread tXMLWriter = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						tMovieFetcher.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Manager.saveToFile(movieList);
				}
			});

			@Override
			// TODO Auto-generated method stub
			public void actionPerformed(ActionEvent arg0) {
				AFetchMoviesFromIMDB movieFetcher = new Fetcher();
//				movieFetcher.setUnfetchedMovies(movieList, movieListGui);
//				tMovieFetcher = new Thread(movieFetcher);
//				tMovieFetcher.start();
//				movieList = movieFetcher.();
				System.out.println(tXMLWriter.getState().toString());
				tXMLWriter.start();

			}
		});
		menu.add(imdbInforFetch);

		JMenuItem rescanDir = new JMenuItem("Rescan Directory");
		menu.add(rescanDir);

		// SplitPane split screen
		JSplitPane splitPane = new JSplitPane();
		getContentPane().add(splitPane, BorderLayout.CENTER);

		// Sticking together
		splitPane.add(movieInfo, JSplitPane.LEFT);
		splitPane.add(movieListGui, JSplitPane.RIGHT);
		setJMenuBar(menuBar);

		// startup
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new ManageMovieGui();
	}
}
